package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.dao.CategoryDao;
import com.itheima.entity.Category;
import com.itheima.entity.Dish;
import com.itheima.entity.Setmeal;
import com.itheima.exception.CustomException;
import com.itheima.service.CategoryService;
import com.itheima.service.DishService;
import com.itheima.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author Tang
 * @date 2023/4/12
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, Category> implements CategoryService {

    @Autowired
    private DishService dishService;

    @Autowired
    private SetmealService setmealService;

    /**
     * 删除分类
     * @param ids
     * @return
     */
    @Override
    public boolean deleteCategory(Long ids) {
        //1.查询菜品表（根据分类id），如果存在数据，不能删除
        LambdaQueryWrapper<Dish> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.eq(Dish::getCategoryId, ids);
        int count1 = dishService.count(wrapper1);
        if (count1 > 0) {
            throw new CustomException("该分类下存在菜品数据，禁止删除！");
        }

        //2.查询套餐表（根据分类id），如果存在数据，不能删除
        LambdaQueryWrapper<Setmeal> wrapper2 = new LambdaQueryWrapper<>();
        wrapper2.eq(Setmeal::getCategoryId, ids);
        int count2 = setmealService.count(wrapper2);
        if (count2 > 0) {
            throw new CustomException("该分类下存在套餐数据，禁止删除！");
        }

        //3.调用自身的删除方法
        return this.removeById(ids);
    }
}
